require(['./config'], () => {
    require(["template", 'jquery', 'bootstrap', 'footer',"cookie"], (template) => {
        class Cart {
            constructor() {
               this.init()
               if(this.cart){
                this.empty()
                this.add()
                this.reduce()
                this.del()
                this.calTotalMoney()
                this.checkedChange()
                this.setAllCheckStatus()
                this.final()
               }
            }
            init(){
                const cart=localStorage.getItem('cart')
                if(cart){
                  this.cart =JSON.parse(cart)
                  $('tbody').html(template("tbody-template", {
                    cart: this.cart
                }))
                }else{
                    $('.empty').show()
                    $('.cart-fast').hide()
                    $('table').hide()
                }
            }
            empty(){
                console.log($('.empty'));
                console.log(this.cart=='');
                if(this.cart==""){
                    $('.empty').show()
                    $('table').hide()
                    $('.cart-fast').hide()

                }
            }
            add(){
                const _this=this
                $(".add").on('click',function(){
                    const id=$(this).parents('tr').data('id')
                    _this.cart=_this.cart.map(shop=>{
                        if(shop.id===id){
                            shop.count+=1
                            $(this).prev().html(shop.count)
                            $(this).parents('tr').find(".danjia").html((shop.count*shop.price).toFixed(2))
                        }
                        return shop
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cart))
                    _this.calTotalMoney()
                })
            }
            reduce(){
                const _this=this
                $(".reduce").on('click',function(){
                    const id=$(this).parents('tr').data('id')
                    _this.cart=_this.cart.map(shop=>{
                        if(shop.id===id&&shop.count>0){
                            shop.count-=1
                            $(this).next().html(shop.count)
                            $(this).parents('tr').find(".danjia").html((shop.count*shop.price).toFixed(2))
                        }
                        return shop
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cart))
                    _this.checkedChange()
                    _this.calTotalMoney()
                })
            }
            del(){
                const _this=this
                $(".del").on('click',function(){
                    const id=$(this).parents('tr').data('id')
                    $(this).parents('tr').remove()
                    _this.cart=_this.cart.filter((shop)=>{
                        return shop.id!=id;
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cart))
                    _this.calTotalMoney()
                    _this.empty()

                })
            }
            setAllCheckStatus(){
                const _this=this
               const isAllCheck=this.cart.every(shop=>shop.check)
               $('.AllCheck').prop('checked',isAllCheck)

              $('.AllCheck').on('click',function(){
                   if($('.AllCheck').prop('checked')){
                    _this.cart=_this.cart.map(shop=>{
                        if(shop.check===false){
                            shop.check=true
                        }
                        return shop
                       })
                       $('.checks').prop('checked',true)
                   }else{
                    _this.cart=_this.cart.map(shop=>{
                        if(shop.check===true){
                            shop.check=false
                        }
                        return shop
                       })
                       $('.checks').prop('checked',false)
                   }
                   localStorage.setItem('cart',JSON.stringify(_this.cart))
                   _this.calTotalMoney()
              })
            }
            checkedChange(){
                const _this=this;
                $('.checks').on('change',function(){
                    const id=$(this).parents('tr').data('id')
                    _this.cart=_this.cart.map(shop=>{
                        if(shop.id===id){
                            shop.check=$(this).prop('checked')
                        }
                        return shop
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cart))
                    _this.setAllCheckStatus()
                    _this.calTotalMoney()
                })
            }
            calTotalMoney(){
               let allMoney=this.cart.reduce((money,shop)=>{
                    if(shop.check) money+=shop.price*shop.count
                    return money
                },0)
                $('.zong1').html(allMoney.toFixed(2))
                $('.zong2').html(allMoney.toFixed(2))
            }
            final(){
            const name=$.cookie("name")
            console.log(name);
            $('.final').on("click",()=>{
                if(name){
                  
                }else{
                  alert("你还没有登录请登录")
                  location.href = '/html/login.html'
                }
            })
            }
        }
        new Cart()
    })
})